package com.utils;


import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author ASUS
 */
public class DateUtils {

    private static final int TWELVE = 12;

    /**
     * 时间转换为字符串
     *
     * @param date 时间
     * @return 时间字符串
     */
    public static String dateToString(Date date) {
        if (date == null) {
            return "";
        }
        String formatStr = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        return sdf.format(date);
    }

    /**
     * 时间转换为字符串
     *
     * @param date      时间
     * @param formatStr 转换格式
     * @return 时间字符串
     */
    public static String dateToString(Date date, String formatStr) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        return sdf.format(date);
    }

    /**
     * 字符串转换为时间
     *
     * @param date 时间字符串
     * @return 时间
     */
    public static Date stringToDate(String date) {
        String formatStr = "yyyy-MM-dd HH:mm";
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        try {
            Date resultDate = sdf.parse(date);
            return resultDate;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 字符串转换为时间
     *
     * @param date      时间字符串
     * @param formatStr 格式
     * @return 时间
     */
    public static Date stringToDate(String date, String formatStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        try {
            Date resultDate = sdf.parse(date);
            return resultDate;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 比较日期1是否小于日期2
     *
     * @param d1 ""
     * @param d2 ""
     * @return true
     */
    public static boolean compareDay(Date d1, Date d2) {
        return (d1.getTime() < d2.getTime());
    }


    /**
     * 时间增加
     *
     * @param date       时间
     * @param scale      增加类型（分，小时，天，月）
     * @param timeLength 增加量
     * @return ""
     */
    public static Date addDate(Date date, String scale, int timeLength) {
        if (timeLength == 0) {
            return date;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int calType = 0;
        switch (scale) {
            case "I":
                calType = Calendar.MINUTE;
                break;
            case "H":
                calType = Calendar.HOUR;
                break;
            case "D":
                calType = Calendar.DATE;
                break;
            case "M":
                calType = Calendar.MONTH;
                break;
            default:
        }
        cal.add(calType, timeLength);
        return cal.getTime();
    }


    public static String[] getLast12Months() {
        String[] last12Months = new String[12];
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
        for (int i = 0; i < TWELVE; i++) {
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
            Date dBefore = cal.getTime();
            //设置时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String defaultStartDate = sdf.format(dBefore);
            last12Months[11 - i] = defaultStartDate;
        }
        return last12Months;
    }

    public static String getMonth(String str) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(sdf.parse(str));
        SimpleDateFormat sdf1 = new SimpleDateFormat("M月");
        Date date = c.getTime();
        return sdf1.format(date);
    }

    public static Date getTomorrow(Date date) {
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, 1);
        Date tomorrow = c.getTime();
        return tomorrow;
    }


}
